package com.example.admin.goodnews.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.Image;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.example.admin.goodnews.R;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * 用户个人页面
 * Created by admin on 2016/9/29.
 */

public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView iv_back_userinfo;// 标题栏返回按钮
    private RelativeLayout tv_logout_userinfo;// 注销按钮
    private RelativeLayout relativeLayout_userinfo;// 页面根布局
    private CircleImageView civ_header_userinfo;// 用户头像
    private TextView tv_nickname_userinfo;// 用户昵称
    private TextView tv_followcount_userinfo;// 用户关注数
    private TextView tv_collectioncount_userinfo;// 用户收藏数

    private LinearLayout takephoto;// PopupWindow中拍照
    private LinearLayout pickphoto;// PopupWindow中选择
    private LinearLayout cancel;// PopupWindow中取消

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        iv_back_userinfo = (ImageView) findViewById(R.id.iv_back_userinfo);// 标题栏返回按钮
        tv_logout_userinfo = (RelativeLayout) findViewById(R.id.tv_logout_userinfo);// 注销按钮
        relativeLayout_userinfo = (RelativeLayout) findViewById(R.id.relativeLayout_userinfo);// 页面根布局
        civ_header_userinfo = (CircleImageView) findViewById(R.id.civ_header_userinfo);// 用户头像
        tv_nickname_userinfo = (TextView) findViewById(R.id.tv_nickname_userinfo);// 用户昵称
        tv_followcount_userinfo = (TextView) findViewById(R.id.tv_followcount_userinfo);// 用户关注数
        tv_collectioncount_userinfo = (TextView) findViewById(R.id.tv_collectioncount_userinfo);// 用户收藏数
    }

    private void initData() {

    }

    private void initEvent() {
        iv_back_userinfo.setOnClickListener(UserInfoActivity.this);// 标题栏返回按钮
        tv_logout_userinfo.setOnClickListener(UserInfoActivity.this);// 注销按钮
        civ_header_userinfo.setOnClickListener(UserInfoActivity.this);// 用户头像
        tv_nickname_userinfo.setOnClickListener(UserInfoActivity.this);// 用户昵称
        tv_followcount_userinfo.setOnClickListener(UserInfoActivity.this);// 用户关注数
        tv_collectioncount_userinfo.setOnClickListener(UserInfoActivity.this);// 用户收藏数
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 标题栏返回按钮
            case R.id.iv_back_userinfo:
                finish();
                break;
            // 标题栏注销按钮
            case R.id.tv_logout_userinfo:
                ShowDialog();
                break;
            // 用户头像
            case R.id.civ_header_userinfo:
                ShowPopupWindow();
                break;
            // 用户昵称
            case R.id.tv_nickname_userinfo:
                break;
            // 用户关注数
            case R.id.tv_followcount_userinfo:
                break;
            // 用户收藏数
            case R.id.tv_collectioncount_userinfo:
                break;
        }
    }

    // 点击注销弹出对话框
    private void ShowDialog() {
        AlertDialog.Builder adb = new AlertDialog.Builder(UserInfoActivity.this);
        adb.setTitle("确认要离开吗");
        adb.setIcon(R.mipmap.ic_header);
        adb.setMessage("嘤嘤嘤，不要离开我~TAT");
        adb.setCancelable(false);
        adb.setNegativeButton("容我三思", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Snackbar.make(relativeLayout_userinfo, "欢迎回来~", Snackbar.LENGTH_SHORT).show();
            }
        });
        adb.setPositiveButton("残忍离开", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Snackbar.make(relativeLayout_userinfo, "注销成功", Snackbar.LENGTH_SHORT).show();
            }
        });
        adb.create();
        adb.show();
    }

    // 选取头像图片的PopupWindow
    private void ShowPopupWindow() {
        final PopupWindow popupwindow = new PopupWindow(UserInfoActivity.this);
        View view_popupwindow = View.inflate(UserInfoActivity.this, R.layout.item_popupwindow, null);
        popupwindow.setContentView(view_popupwindow);
        popupwindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出的PopupWindow的宽度
        popupwindow.setHeight(300);// 设置PopupWindow的高度
        popupwindow.setOutsideTouchable(true);// 设置点击PopupWindow外部可隐藏PopupWindow
        View rootview = View.inflate(UserInfoActivity.this, R.layout.activity_userinfo, null);
        popupwindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
        takephoto = (LinearLayout) view_popupwindow.findViewById(R.id.takephoto);// PopupWindow中拍照
        pickphoto = (LinearLayout) view_popupwindow.findViewById(R.id.pickphoto);// PopupWindow中选择
        cancel = (LinearLayout) view_popupwindow.findViewById(R.id.cancel);// PopupWindow中取消
        // PopupWindow中拍照
        takephoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_takephoto = new Intent();
                
            }
        });
        // PopupWindow中选择
        pickphoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        // PopupWindow中取消
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupwindow.dismiss();
            }
        });
    }

}
